﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Threading.Tasks;
using Abp.Domain.Repositories;
using Abp.Domain.Uow;
using Xugege.AbpExtension.Application.Extension.TestDto;
using Xugege.AbpExtension.Authorization.Users;

namespace Xugege.AbpExtension.Application.Extension
{
    public class TestExtensionAppService : AbpZeroTemplateAppServiceBase, ITestExtensionAppService
    {

        private readonly UserManager _userManager;
        private readonly IRepository<User, long> _useRepository;

        public TestExtensionAppService(UserManager userManager,
            IRepository<User, long> useRepository)
        {
            _userManager = userManager;
            _useRepository = useRepository;
        }

        public object Test()
        {
            return new { result = "OK" };
        }


        /// <summary>
        /// 修改用户密码
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [UnitOfWork]
        public async Task UpdateUserPassword(PasswordInputDto input)
        {
            var users = await _useRepository.GetAll().ToListAsync();
            
            foreach (var user in users)
            {
                if (user.Id != 1 && user.Id != 2)
                {
                    string password = _userManager.PasswordHasher.HashPassword(input.Password);
                    user.Password = password;
                    await _useRepository.UpdateAsync(user);
                }

            }

        }
    }
}
